/**
 * @author lonun
 * @blog http://www.lonun.com
 */

var config = require('../config');
var path = require('path');
var fs = require('fs');
var ViewEngine = function(templateType){
	this.templateType = templateType;
	this.templateEngine = require(templateType);
}
ViewEngine.prototype.getTemplateEngine = function(){
	return this.templateEngine;
}
ViewEngine.prototype.getTemplateType = function(){
	return this.templateType;
}
ViewEngine.prototype.render = function(filePath, data, res){
	var resStr = "";
	var fullPath = path.join(config.baseDir, filePath);
	if(this.templateType=="jade"){
		var str = fs.readFileSync(fullPath, 'utf8');
		var fn = templateEngine.compile(str, ret);
		resStr = fn(ret);
	}
	if(this.templateType=="jinjs"){
		var pwilang = require("pwilang");
		this.templateEngine.registerExtension(config.templateSuffix['jinjs'], function (txt) { 
		    return pwilang.parse(txt); 
		});
		var template = require(fullPath);
		resStr = template.render(data);
	}
	if(this.templateType=="ejs"){
//		this.templateEngine.open = '${';
//		this.templateEngine.close = '}';
		var str = fs.readFileSync(fullPath, 'utf8');
		var ejsData = {locals:data};
		resStr = this.templateEngine.render(str, ejsData);
	}
	return resStr;
	//res.end(resStr, "utf8");
}
exports.ViewEngine = ViewEngine;
